/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.ClassBean;
import model.dao.ClassDAO;
import model.util.PagingUtil;
import model.util.SharingUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Aptech
 */
public class ClassAction extends BaseAction {

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute(BaseAction.CONTENT_HOLDER, "class-add");
        return mapping.findForward("home");
    }
    public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute(BaseAction.CONTENT_HOLDER, "index");
        int id=Integer.parseInt(request.getParameter("selected-id").equals("")?"0":request.getParameter("selected-id"));
        ClassDAO daoClass=new ClassDAO();
        SharingUtil.setCurrentClass(request.getSession(), daoClass.getClass(id));
        return mapping.findForward("home");
    }
    public ActionForward manage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ClassDAO daoClass = new ClassDAO();
        ArrayList<model.dto.Class> list = null;
        request.setAttribute(BaseAction.CONTENT_HOLDER, "class-manage");
        int totalRecords = 0;
        int currentPage = PagingUtil.getPageNumber(request.getParameter("page"));
        if (SharingUtil.getUser(request.getSession()).getRole() == 1) {
            list = daoClass.getClasses(currentPage, PagingUtil.CONST_PAGE_SIZE);
            totalRecords = daoClass.getTotalRecords();
        } else {
            list = daoClass.getClassesOfUser(currentPage, PagingUtil.CONST_PAGE_SIZE, SharingUtil.getUser(request.getSession()).getId());
        }
        request.setAttribute("list", list);
        request.setAttribute("PagingNav", PagingUtil.getPageNavigator(totalRecords, PagingUtil.CONST_PAGE_SIZE, currentPage, "?page="));
        //request.setAttribute("list", daoClass.ge);
        return mapping.findForward("home");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        int classId = Integer.parseInt(request.getParameter("id") == null ? "0" : request.getParameter("id"));
        ClassDAO daoClass = new ClassDAO();
        model.dto.Class cls = daoClass.getClass(classId);
        request.setAttribute(BaseAction.CONTENT_HOLDER, "class-edit");
        if (cls.getId() > 0) {
            request.setAttribute("class", cls);
        } else {
            SharingUtil.showMessage(request, new ActionMessage("edit.failure"));
        }
        return mapping.findForward("home");
    }

    public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ClassDAO daoClass = new ClassDAO();
        model.dto.Class cls = new model.dto.Class();
        ClassBean bnClass = (ClassBean) form;
        cls.setName(bnClass.getName());
        cls.setIsPublic(bnClass.getIsPublic());
        cls.setCreatedDate(new Date());
        cls.setTeacher(SharingUtil.getUser(request.getSession()));        
        if (daoClass.create(cls) > 0) {
            SharingUtil.showMessage(request, new ActionMessage("create.success"));
        } else {
            SharingUtil.showMessage(request, new ActionMessage("create.failure"));
        }
        request.setAttribute(BaseAction.CONTENT_HOLDER, "class-create");
        return mapping.findForward("home");
    }

    public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ClassDAO daoClass = new ClassDAO();
        model.dto.Class cls = new model.dto.Class();
        ClassBean bnClass = (ClassBean) form;
        cls.setName(bnClass.getName());
        cls.setIsPublic(bnClass.getIsPublic());
        cls.setCreatedDate(new Date());
        cls.setTeacher(SharingUtil.getUser(request.getSession()));
        if (daoClass.create(cls) > 0) {
            SharingUtil.showMessage(request, new ActionMessage("update.success"));
        } else {
            SharingUtil.showMessage(request, new ActionMessage("update.failure"));
        }
        request.setAttribute(BaseAction.CONTENT_HOLDER, "class-manage");
        return mapping.findForward("home");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        ClassDAO daoClass = new ClassDAO();
        int classId = Integer.parseInt(request.getParameter("selected-id") == null ? "0" : request.getParameter("selected-id"));
        if (daoClass.delete(classId) > 0) {
            SharingUtil.showMessage(request, new ActionMessage("delete.success"));
        } else {
            SharingUtil.showMessage(request, new ActionMessage("delete.failure"));
        }
        request.setAttribute(BaseAction.CONTENT_HOLDER, "class-manage");
        return mapping.findForward("home");
    }
}
